package com.example.uiview_025;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class GralleryActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_grallery);
		Gallery gallery = (Gallery) findViewById(R.id.gallery1);
		final int[] images = new int[]{
				R.drawable.c1,
				R.drawable.c2,
				R.drawable.c3,
				R.drawable.c4,
		};
		gallery.setAdapter(new MyAdapter(images, this));
		gallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				ImageView imageView = (ImageView) findViewById(R.id.imageView1);
				imageView.setImageResource(images[arg2]);
			}
		});
	}
	
	private class MyAdapter extends BaseAdapter{
		private int[] images;
		private Context context;
		public MyAdapter(int[] images,Context context) {
			super();
			this.context = context;
			this.images = images;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return images.length;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			ImageView imageView = new ImageView(context);
			imageView.setImageResource(images[arg0]);
			imageView.setLayoutParams(new Gallery.LayoutParams(120,120));
			return imageView;
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.grallery, menu);
		return true;
	}

}
